package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.web.dao.CommonDao;
import com.ys.web.entity.Role;
import com.ys.web.entity.User;
import com.ys.web.entity.UserRole;
import com.ys.web.pojo.UserInfo;
import com.ys.web.service.UserService;

/**
 * 用户表 -Service-实现类
 * @author wdj
 *
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private CommonDao commonDao;

	@Override
	public void delete(Integer id) {
		commonDao.executeHql("delete User where id="+id);
		
	}

	@Override
	public Integer add(UserInfo userinfo) {
		User user = new User();
		BeanUtils.copyProperties(userinfo,user);
		user.setRegisterTime(new Date()); 
		user.setUpdateTime(new Date());
		Integer userId =(Integer)commonDao.save(user);
		//List<String> roleIds = userinfo.getRoleIds();
		String[] roleIds = null;
		if(userinfo.getRoleIds()!=null){
			roleIds=userinfo.getRoleIds().split(",");
		}
		//roleIds = userinfo.getRoleIds();
		if(roleIds!=null){
			for(String roleid:roleIds){
				UserRole userRole = new UserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(Integer.valueOf(roleid));
				commonDao.save(userRole);
			}
		}
		
		
		return userId;
	}

	@Override
	public void save(UserInfo userinfo) {
		User user = new User();
		BeanUtils.copyProperties(userinfo,user);
		user.setRegisterTime(new Date()); 
		user.setUpdateTime(new Date());
		commonDao.saveOrUpdate(user);;
		//List<String> roleIds = userinfo.getRoleIds();
		
		String[] roleIds = null;
		if(userinfo.getRoleIds()!=null){
			roleIds= userinfo.getRoleIds().split(",");
		}
		
		//roleIds = userinfo.getRoleIds();
		commonDao.executeHql("delete UserRole where userId="+user.getId());
		if(roleIds!=null){
		for(String roleid:roleIds){
			
			
			UserRole userRole = new UserRole();
			userRole.setUserId(user.getId());
			userRole.setRoleId(Integer.valueOf(roleid));
			commonDao.save(userRole);
		}
		}
		
	}

	@Override
	public UserInfo getUserInfo(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserInfo> getUserInfoPage(int page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserInfo> getAllUserInfo() throws Exception {
		//List<UserInfo> userInfos = new ArrayList<UserInfo>();
		//commonDao.findHql("from User", null);
		List<UserInfo> userinfos = new ArrayList<UserInfo>();
		List<User> users = commonDao.findHql("from User");
		for(User user:users){
			UserInfo userInfo = new UserInfo();
			//List<UserRole> userRoles = new ArrayList<>();
			List<UserRole> userRoles = commonDao.findHql("from UserRole where userId=?",user.getId());
			StringBuffer roleBuffer = new StringBuffer();
			StringBuffer roleIdRuffer = new StringBuffer();
			userInfo.setId(user.getId());
			userInfo.setUsername(user.getUsername());
			userInfo.setPassword(user.getPassword());
			userInfo.setEmployeeName(user.getEmployeeName());
			userInfo.setEmployeeNumber(user.getEmployeeNumber());
			for(UserRole userRole:userRoles){
				Role role = commonDao.get(Role.class, userRole.getRoleId());
				roleBuffer.append(role.getRoleName()+",");
				roleIdRuffer.append(role.getId()+",");
				
			}
			
			userInfo.setRoleIds(roleIdRuffer.toString());
			
			userInfo.setRoles(roleBuffer.toString());
			userinfos.add(userInfo);
		
		}
		
		return userinfos;
	}

	@Override
	public List<UserInfo> getAllSemUser() throws Exception {
		List<UserInfo> userinfos = new ArrayList<UserInfo>();
		List<UserRole> userRoles = commonDao.findHql("from UserRole where roleId = 3");
		for(UserRole userRole:userRoles){
			
			User user =commonDao.get(User.class, userRole.getUserId());
			if(user!=null){
			UserInfo userinfo = new UserInfo();
			BeanUtils.copyProperties(user, userinfo);
			userinfos.add(userinfo);	
			}
		}
		
		return userinfos;
	}

	@Override
	public User getUserByUsername(String username) throws Exception {
		User user =commonDao.findUniqueByProperty(User.class, "username", username);
		return user;
	}

	@Override
	public Boolean isUsernameExist(String username) throws Exception {
		// TODO Auto-generated method stub
		if(getUserByUsername(username)==null){
			return false;
		}
		return true;
	}
	
 	/**
 	 * 通过员工名称获取用户
 	 * @param employeeName
 	 * @return
 	 * @throws Exception
 	 */
	@Override
 	public User getUserByEmployeeName(String employeeName) throws Exception {
		User user =commonDao.findUniqueByProperty(User.class, "employeeName", employeeName);
		return user;				
	}
		
	
}
